//
//  UIColor+Extension.swift
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/11.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

extension UIColor {
    convenience init(hex: String) {
        let scanner = Scanner(string: hex)
        scanner.currentIndex = hex.startIndex
        
        var hexNumber: UInt64 = 0
        
        // 扫描十六进制值
        scanner.scanHexInt64(&hexNumber)
                
        // 根据十六进制颜色值的长度来决定RGBA的值
        let r = (hexNumber & 0xFF0000) >> 16
        let g = (hexNumber & 0x00FF00) >> 8
        let b = hexNumber & 0x0000FF
        let a = hex.count == 8 ? (hexNumber & 0x000000FF) >> 24 : 0xFF
 
        // 创建UIColor实例
        self.init(
            red: CGFloat(r) / 0xFF,
            green: CGFloat(g) / 0xFF,
            blue: CGFloat(b) / 0xFF,
            alpha: CGFloat(a) / 0xFF
        )
    }
}
